package mediatheque.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import tools.DoubleClicEvent;
import tools.DoubleClicListener;

import mediatheque.model.MediathequeModel;
import mediatheque.view.RechercheView;

public class RechercheController extends AbstractController {

	private RechercheView view;
	
	public RechercheController(final MediathequeModel model, final RechercheView view) {
		super(model);
		this.view = view;
		
		view.addRechercherDocumentListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				rechercher();
			}
		});
		
		view.addChangeTypeListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				rechercher();
			}
		});
		
		view.addChangeChampsRechercheListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				rechercher();
			}
			@Override
			public void insertUpdate(DocumentEvent e) {
				rechercher();
			}			
			@Override
			public void changedUpdate(DocumentEvent e) {
				rechercher();
			}
		});
		
		view.addClicListener(new MouseAdapter() {			
			public void mousePressed(MouseEvent e) {
				view.afficherDetails(view.getDocument(e.getPoint()));
			};
		});
		
		
	}

	protected void rechercher() {
		String id = view.getValeurIdentifiant();
		String nom = view.getValeurNom();
		String desc = view.getValeurDescription();
		String type = view.getValeurType();
		String date = view.getValeurDate();		
		view.setListeDocuments(model.rechercheDocuments(id, nom, desc, type, date));
	}
	
	
	
	
}
